﻿using System;
using System.Data;
using System.Text.RegularExpressions;
using System.Windows;

namespace PSSMS.View.SysManage
{
    /// <summary>
    /// ProviderAdd.xaml 的交互逻辑
    /// </summary>
    public partial class ProviderAdd : Window
    {
        BLL.Providersm.ProvidersmClient myClient = new BLL.Providersm.ProvidersmClient();
        /// <summary>
        /// 声明全局变量
        /// </summary>
        DataTable dtStation;//记录表格数据
        public ProviderAdd()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 构造器
        /// </summary>
        /// <param name="strNumber"></param>
        public ProviderAdd(string strNumber)
        {
            InitializeComponent();
            //strStaffNumber = strNumber;//获取最大编号
        }

        /// <summary>
        /// 窗口事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //下拉框绑定
            DataTable dt = myClient.SelectSrType().Tables[0];
            ghslx.ItemsSource = dt.DefaultView;
            ghslx.DisplayMemberPath = "supplierType";
            ghslx.SelectedValuePath = "supplierTypeID";

            DataTable ds = myClient.SelDisName().Tables[0];
            ssdq.ItemsSource = ds.DefaultView;
            ssdq.DisplayMemberPath = "disName";
            ssdq.SelectedValuePath = "disID";

            DataTable dd = myClient.SelectPmethod().Tables[0];
            jsfs.ItemsSource = dd.DefaultView;//显示数据
            jsfs.DisplayMemberPath = "paymentMethod";
            jsfs.SelectedValuePath = "paymentMethodID";
        }

        /// <summary>
        /// 验证期初付款价格只能输入数值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void jsfs_LostFocus(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(qcfk.Text))
                {
                    //浮点数正则表达式
                    if (!Regex.IsMatch(qcfk.Text.Trim(), @"^[1-9]\d*|^[1-9]\d*.\d+[1-9]$"))
                    {
                        MessageBox.Show("期初付款价格只能是金额数值！", "系统提示", MessageBoxButton.OKCancel, MessageBoxImage.Error);
                        qcfk.Text = string.Empty;
                    }
                }
            }
            catch (Exception)
            {
                return;
            }
        }
        /// <summary>
        /// 确定、保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Save_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //1、必填项不能为空
                if (!string.IsNullOrEmpty(ghsmc.Text.Trim()))
                {
                    //2、获取控件值
                    string strSupplierName = ghsmc.Text.Trim();
                    string strContactPerson = lxr.Text.Trim();//联系人
                    string strContactNumber = lxdh.Text.Trim();//联系电话 
                    int intSupplierTypeID = Convert.ToInt32(ghslx.SelectedValue);//供货商类型
                    string strRemark = bz.Text.Trim();//备注
                    int intDisID = Convert.ToInt32(ssdq.SelectedValue);//所属地区
                    decimal decInitialPayable = Convert.ToDecimal(qcfk.Text);//期初应付
                    int intPaymentMethodID = Convert.ToInt32(jsfs.SelectedValue);//结算方式
                    string strContactAddress = lxdz.Text.Trim();//联系地址
                    bool blDefaultNo = (bool)mrgys.IsChecked;//默认
                    bool blDisabledNo = (bool)jy.IsChecked;//启用、禁用

                    //3、新增信息：获取返回值,与方法一样的参数
                    int intCount = myClient.InsertManage(strSupplierName, strContactPerson,
             strContactNumber, intSupplierTypeID, strRemark, intDisID, decInitialPayable,
             intPaymentMethodID, strContactAddress, blDefaultNo, blDisabledNo);
                    if (intCount > 0)
                    {
                        MessageBoxResult dr = MessageBox.Show("新增成功！", "系统提示", MessageBoxButton.OKCancel,MessageBoxImage.Information); //弹出确定对话框
                        this.Close();
                    }
                    else if (intCount == -1)
                    {
                        MessageBox.Show("新增信息重复！", "系统提示", MessageBoxButton.OKCancel, MessageBoxImage.Error);

                    }
                    else
                    {
                        MessageBox.Show("新增失败！", "系统提示", MessageBoxButton.OKCancel, MessageBoxImage.Error);
                    }
                }
                else
                {
                    MessageBox.Show("请输入供货商名称，并检查是否输入完整！", "系统提示：", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Cancel_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("关闭窗口，无法保存数据，是否要关闭？", "系统提示：", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
            {
                //关闭窗口
                this.Close();
            }
        }
    }
}
